<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file license.txt or that can be found at 
 * http://code.google.com/p/billboardce/wiki/License.
 *
 * @package    Slide_Stream_Map
 * @category   Model
 * @license    http://code.google.com/p/billboardce/wiki/License New BSD License
 * @version    SVN: $Id: $
 */
class Slide_Stream_Map extends Ot_Db_Table
{
    /**
     * Name of the table in the database
     *
     * @var string
     */
    protected $_name = 'tbl_slide_stream_map';
  
    /**
     * Primary key of the table
     *
     * @var string
     */
    protected $_primary = 'slideStreamMapId';
    
    /**
     * Returns an array of all the slides & slideStreams that are in a given 
     * slide stream ID
     * 
     * @param int $slideStreamId The slide stream ID to get the entities for
     * @return array The entities in the slide stream
     */
    public function getEntitiesForSlideStream($slideStreamId) {
        
        $entities = $this->fetchAll($this->getAdapter()->quoteInto('slideStreamId = ?', $slideStreamId), 'order DESC')->toArray();
        
        $entityList = array();
        
        $slide = new Slide();
        $slideStream = new Slide_Stream();
        $optionPlugin = new Plugin_Slide_Option();
        
        $optionModules = $optionPlugin->getEnabledModules('slide');
        
        foreach ($entities as $e) {
            
            $tmpData = array();
            
            if ($e['entityType'] == 'slide') {
                
                $tmpData = $slide->find($e['entityTypeId']);

            } else if ($e['entityType'] == 'slideStream') {
                $tmpData = $slideStream->find($e['entityTypeId']);
            }
            
            if (!is_null($tmpData)) {
                
                $e['data'] = $tmpData->toArray();
                
                $e['moduleData'] = array();
            
                foreach ($optionModules as $o) {
                    $e['moduleData'][$o['name']] = $o['object']->getData($e['entityTypeId']);
                }
                
                $typeObj = Plugin_Slide_Type::factory($e['data']['slideType']);
                
                $e['typeData'] = $typeObj->getData($e['entityTypeId']);
                $e['displayUrl'] = $typeObj->getUrl($e['entityTypeId']);
                
                $entityList[] = $e;
            }
        }

        return $entityList;
    }
    
    
    /**
     * Saves the data for a slidestream.  The data is a keyed array of slides like:
     * 
     * slideId       => 56
     * order         => 1
     * url           => http://www.yahoo.com
     * title         => Yahoo
     * displayLength => 5
     * 
     * @param int $slideStreamId The slide stream ID to save
     * @param array The array of slides to saves
     */
    public function saveSlideStream($slideStreamId, $data)
    {
        $slide = new Slide();
        
        $db = $this->getAdapter();
        
        $db->beginTransaction();
        
        $optionPlugin = new Plugin_Slide_Option();
        
        $optionModules = $optionPlugin->getEnabledModules('slide');
        
        foreach ($data as $d) {
            
            $slideMapData = array(
                                'order'         => $d['order'],
                                'displayLength' => $d['displayLength']
                            );
                            
            $where = $db->quoteInto('slideStreamId = ?', $slideStreamId)
                   . ' AND '
                   . $db->quoteInto('entityType = ?', 'slide') // @todo this will need to be changed to support either slides or slidestreams later
                   . ' AND '
                   . $db->quoteInto('entityTypeId = ?', $d['slideId']);
                            
            try {
                $this->update($slideMapData, $where);
            } catch (Exception $e) {
                $db->rollBack();
                throw new Ot_Exception_Data('Error saving slide stream mapping. ' . $e->getMessage());
            }
            
            $slideData = array(
                            'slideId' => $d['slideId'],
                            'title'   => $d['title']
                         );
            try {
                $slide->update($slideData, null);
            } catch (Exception $e) {
                $db->rollBack();
                throw new Ot_Exception_Data('Error saving slide in slide stream. ' . $e->getMessage());
            }
            
            $typeObj = Plugin_Slide_Type::factory($d['slideType']);
            
            try {
                $typeObj->saveTypeForm($d['slideId'], $d['typeData']);
            } catch (Exception $e) {
                $db->rollBack();
                throw new Ot_Exception_data('Error saving slide type settings. ' . $e->getMessage());
            }            

            // save option module data
            foreach ($optionModules as $o) {
                if (isset($d['moduleData'][$o['name']]) && !empty($d['moduleData'][$o['name']])) {                
                    
                    try {
                        $o['object']->saveOptionForm($d['slideId'], $d['moduleData'][$o['name']]);
                    }
                    catch (Exception $e) {
                        $db->rollBack();
                        throw new Ot_Exception_Data('Error saving slide module data. ' . $e->getMessage());
                    }
                }
            }

        }
        
        $db->commit();
    }
}